// pages/login/login.js
var duixiang=require("../tool/come");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getpic:true,
    base64:'',
    result:'',
    checkcode:''
  },
  opengetpic:function(){
    this.setData({
      getpic:false
    })
  },
  //验证验证码是否正确
  checkCode:function(event){
    if(event.detail.value==this.data.checkcode){
      this.setData({
         result:"../../images/great.png"
      })
    }else{
      this.setData({
        result:"../../images/fault.png"
     })
    }
  },
  formsubmit:function(options){
     if(options.detail.target.dataset.type=="checkCode"){
      var temp=Math.floor(Math.random()*10000)+"";
      for(var i=0;i<4-temp.length;i++){
        temp+=0;
      }
      this.setData({
        checkcode:temp
      })
     }else if(options.detail.target.dataset.type=="submit"){
      //调用云数据库
      this.setData({
        result:''
     })
       var nickname=options.detail.value.uname;
       var phone=options.detail.value.uphone;
       var base64=this.data.base64;
       var code=options.detail.value.ucheckcode;
       if(nickname==''||phone==''||base64==''||code=='')
       {
        wx.showToast({
          title: '入库失败',
        })
       }
       else{
       wx.cloud.database().collection("shibie").add({
         data:{
          nickname:nickname,
          phone:phone,
          base64:base64
         },
         success:function(res){
           wx.showToast({
             title: '入库成功',
           })
         }
       })
      }
     }
  },
  //拍照并将图片转换成64位
  getPicBase64:function(){
    var that=this;
    const ctx=wx.createCameraContext();
    duixiang.getPicFromcenavm(ctx).then(res=>{
      that.setData({
        base64:res,
        getpic:true
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

})